package com.cnhis.cloudhealth.clinical.hosnurse.hosadvicevaluationadjust.bo;

import java.util.*;

import javax.annotation.Resource;

import com.cnhis.cloudhealth.commons.utils.Page;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hosadvicevaluationadjust.dao.HosAdviceValuationAdjustDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

import net.sf.json.JSONObject;

@Service
public class HosAdviceValuationAdjustBo {
 @Resource
 private HosAdviceValuationAdjustDao hosAdviceValuationAdjustDao;
 @Resource
 private GetNewId  GetNewId;
 @SuppressWarnings("unchecked")
public ModelVo adviceValuationAdjust(Map map)throws Exception{
	 ModelVo mo = new ModelVo();
		List list =null;
		try {
			String lDay =map.get("lDay").toString();
			if("0".equals(lDay)){
				map.put("aDate", "1899-12-30");
			}else{
				map.put("aDate",DateUtils.getDateString(DateUtils.addInteger(new Date(),Calendar.DATE,-Integer.parseInt(lDay))));
			}
					
			
			list=hosAdviceValuationAdjustDao.adviceValuationAdjust(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
 }
 @SuppressWarnings("unchecked")
public ModelVo valuationInfo(Map map)throws Exception{
	 ModelVo mo = new ModelVo();
		List list =null;
		try {
			map.put("aDate", DateUtils.getDateStr(new Date())+" 23:59:59.999");
			map.put("pValue", hosAdviceValuationAdjustDao.getValue177());
			list=hosAdviceValuationAdjustDao.valuationInfo(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
 }
 private boolean checkOffice(Map map)throws Exception{
	 return hosAdviceValuationAdjustDao.checkOffice(map)>0;
 }
 private void insertVBD2(Map map)throws Exception{
	 map.put("lVBDid", GetNewId.nextId());
	 hosAdviceValuationAdjustDao.insertVBD2(map);
 }
 @SuppressWarnings("unchecked")
public ModelVo execNurseAdviceCalculate(Map map)throws Exception{
	 ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
	 String lxml = map.get("lXML").toString();
	 JSONObject jsonObject = JSONObject.fromObject(lxml);
	 List VBD2List = JsonUtil.getList(jsonObject.getJSONArray("VBD1").toString());
	 List VAF2List = JsonUtil.getList(jsonObject.getJSONArray("VAF1").toString());
	 map.put("VBD2List", VBD2List);
	 map.put("VAF2List", VAF2List);
	 if(checkOffice(map)){
		 mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("医嘱计价中执行科室不正确，请检查修改");
			return mo;
	 }
	 //保存医嘱计价信息
	 insertVBD2(map);
	 
	 return mo;
 }
	public ModelVo query1055(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosAdviceValuationAdjustDao.query1055(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo collectFeeItemDetail(Map map, int currentPage, int pageSize)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		Object lDate =map.get("lDate");
		try {
			if(lDate==null||"".equals(lDate.toString())){
				map.put("lDate", DateUtils.getDateStr(new Date())+" 23:59:59");
			}else{
				map.put("lDate", lDate+" 23:59:59");
			}
			HashMap hs = new HashMap();
			hs.put("productId",9999);
			hs.put("programId",9999);
			hs.put("paramNo", 15);
			String aParamno = hosAdviceValuationAdjustDao.getSysParamValue(hs);
			Page pg = null;
			if(!"1".equals(aParamno)){
				pg = hosAdviceValuationAdjustDao.queryForPage("HosAdviceValuationAdjust.collectFeeItemParamno1", map, currentPage, pageSize);
			}else{
				pg = hosAdviceValuationAdjustDao.queryForPage("HosAdviceValuationAdjust.collectFeeItemParamno", map, currentPage, pageSize);
			}
			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(pg.getTotalNumber());
			mo.setRows(pg.getList());
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		return mo;
	}

	public ModelVo findDrugSpecification(Map map)
			throws Exception
	{
		ModelVo mo = new ModelVo();
		List list = null;
		try
		{
			list = hosAdviceValuationAdjustDao.findDrugSpecification(map);

		}
		catch (Exception e)
		{
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
}
